// 实现这个项目的构建任务
const { src, dest, parallel, series } = require('gulp');
// 自动加载组件
const loadPlugins = require('gulp-load-plugins');
const plugins = loadPlugins();
const del = require('del');

// 页面数据
const data = {
  menus: [
    {
      name: 'Home',
      icon: 'aperture',
      link: 'index.html'
    },
    {
      name: 'Features',
      link: 'features.html'
    },
    {
      name: 'About',
      link: 'about.html'
    },
    {
      name: 'Contact',
      link: '#',
      children: [
        {
          name: 'Twitter',
          link: 'https://twitter.com/w_zce'
        },
        {
          name: 'About',
          link: 'https://weibo.com/zceme'
        },
        {
          name: 'divider'
        },
        {
          name: 'About',
          link: 'https://github.com/zce'
        }
      ]
    }
  ],
  pkg: require('./package.json'),
  date: new Date()
}

// 清除
const clean = () => {
  return del(['dist'])
}

// 样式编译
const style = () => {
  return src('src/assets/styles/*.scss', { base: 'src'})
    .pipe(plugins.sass())
    .pipe(dest('dist'))
}

// 脚本编译
const script = () => {
  return src('src/assets/scripts/*.js', { base: 'src' })
    .pipe(plugins.babel({ presets: ['@babel/preset-env'] }))
    .pipe(dest('dist'))
}

// 页面编译
const page = () => {
  return src('src/*.html', { base: 'src'})
    .pipe(plugins.swig({ data }))
    .pipe(dest('dist'))
}

// 图片压缩
const image = () => {
  return src('src/assets/images/**', { base: 'src'})
    .pipe(plugins.imagemin())
    .pipe(dest('dist'))
}

// font压缩
const font = () => {
  return src('src/assets/fonts/**', { base: 'src'})
    .pipe(plugins.imagemin())
    .pipe(dest('dist'))
}

// 静态文件
const extra = () => {
  return src('public/**', { base: 'public'})
    .pipe(dest('dist'))
}

// 处理html引入的node-modules的css,js
const useref = () => {
  return src('dist/*.html', { base: 'dist' })
    .pipe(plugins.useref({ searchPath: ['dist', '.'] }))
    .pipe(dest('dist'))
}
// 将任务功能和 / 或组合操作组合成同时执行的较大操作
const complie = parallel(style, script, image, font, page);
// 将任务函数和/或组合操作组合成更大的操作，这些操作将按顺序依次执行
const build = series(clean, parallel(complie, extra), useref)

module.exports = {
  build,
}